<template>
  <div>
    <div class="head">
      <van-nav-bar>
        <template #left>
          <div class="logo">
            <img src="../assets/images/home_active.png" alt="" />
            <span class="logo-title">Luckin Coffee</span>
          </div>
        </template>
        <template #right>
          <div class="goBefore" @click="goLook">先逛一逛</div>
        </template>
      </van-nav-bar>
    </div>
    <div class="login-box">
      <h1 class="login-title">找回密码！</h1>
      <p class="login-subTitle">Find password</p>
      <van-form>
        <van-field
          v-model="mailbox"
          name="邮箱"
          label="邮箱"
          placeholder="邮箱"
          :rules="[
            {
              required: true,
              message: '请填写正确邮箱',
              pattern: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
            },
          ]"
        />
        <van-field
          v-model="sms"
          center
          clearable
          type="text"
          name="验证码"
          label="验证码"
          placeholder="验证码(6个字符)"
        >
          <template #button>
            <van-button
              size="small"
              type="primary"
              color="rgb(12,52,186)"
              :disabled="isClick"
              @click="send"
              >{{ isClick ? num : "发送验证码" }}</van-button
            >
          </template>
        </van-field>
        <div class="login-tip"><span @click="goLogin">已有账号,立即登录</span></div>
        <div style="margin: 16px">
          <van-button
            round
            block
            type="info"
            native-type="submit"
            color="rgb(12,52,186)"
            @click="next"
            >下一步</van-button
          >
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { Toast } from "vant";
export default {
  name: "Forget",
  data() {
    return {
      appkey: "U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=",
      sms: "",
      mailbox: "",
      isClick: false,
      num: 60,
    };
  },
  methods: {
    next() {
      let validCode = this.sms;
      let appkey = this.appkey;

      let data = `appkey=${appkey}&validCode=${validCode}`;
      this.axios({
        method: "post",
        url: "http://www.kangliuyong.com:10002/checkValidCode",
        data,
      })
        .then((res) => {
          
          if (res.data.code === "K001") {
            Toast.success("验证码验证成功");
            this.$router.push({ name: "Reset" });
          } else {
            Toast("验证失败,请输入正确验证码");
          }
        })
        .catch((err) => {
          
        });
    },
    send() {
      
      
      let reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
      if (!reg.test(this.mailbox)) {
        Toast("邮箱输入错误");
      } else {
        this.isClick = true;
        let timer = setInterval(() => {
          this.num--;
          
          if (this.num < 1) {
            clearInterval(timer);
            this.num = 60;
            this.isClick = false;
          }
        }, 1000);
        let appkey = this.appkey;
        let email = this.mailbox;
        let data = `appkey=${appkey}&email=${email}`;
        this.axios({
          method: "post",
          url: "http://www.kangliuyong.com:10002/emailValidCode",
          data,
        })
          .then((res) => {
            
            Toast("验证码已发送,5分钟内有效");
          })
          .catch((err) => {
            
          });
      }
    },
    goLogin() {
      this.$router.push({ name: "Login" });
    },
    goLook() {
      this.$router.push({ name: "Home" });
    },
  },
};
</script>

<style lang="less" scoped>
.head {
  .logo {
    display: flex;
    align-items: center;
    img {
      height: 36px;
    }
    .logo-title {
      font-size: 16px;
      font-weight: bold;
    }
  }
  .goBefore {
    color: rgb(12, 52, 186);
  }
}
.login-box {
  padding: 0 20px;
  margin-top: 30%;
  .login-title {
    color: #666;
  }
  .login-subTitle {
    color: #666;
    font-size: 16px;
  }
  .login-tip {
    text-align: right;
    font-size: 14px;
    line-height: 30px;
    margin-top: 10px;
    margin-bottom: 40px;
    padding-right: 10px;
  }
  .popup-title {
    color: #666;
    font-size: 24px;
    padding: 10px;
    font-weight: bold;
  }
}
/deep/.van-field__label {
  margin: 0;
}
</style>